package Str;

/**
 * @auther wangchenghao
 * @date 2021/6/13
 **/
public class test7 {
    public static int strStr(String haystack, String needle) {
        if (needle==null||needle.length()==0||haystack.equals(needle)){
            return 0;
        }
        if (needle.length()>haystack.length()){
            return -1;
        }
        char []chars1=haystack.toCharArray();
        char []chars2=needle.toCharArray();
        for (int i = 0; i < haystack.length(); i++) {
            int j=0;
            int index=i;
            if (chars1[i]==chars2[j]){
                while (j<needle.length()&&index<haystack.length()){
                    if (chars1[index]==chars2[j]){
                        j++;
                        index++;
                    }else {
                        break;
                    }
                }
                if (j==needle.length()){
                    return i;
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        System.out.println("strStr(\"hello\",\"ll\") = " + strStr("abc", "abc"));
    }
}
